#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

/*******************************************
Copyrigth (c) tiny
file            :dtsled.c
author          :tiny
version         :1.0.0
description     :led driver file
other           :none
log             :first version 2023-03-03
*******************************************/
#define     DTSLED_NAME     "dtsled"
#define     DTSLED_CNT      1
/* define led status */
#define     LED_ON      1
#define     LED_OFF     0

/* define led gpio register address */
/* #define     CCM_CCGR1_BASE              (0x20C406C) */
/* #define     SW_MUX_GPIO5_IO03_BASE      (0x20E0028) */
/* #define     SW_PAD_GPIO5_IO03_BASE      (0x20E02B4) */
/* #define     GPIO5_DR_BASE               (0x20AC000) */
/* #define     GPIO5_GDIR_BASE             (0x20AC004) */

/* define led gpio remap address */
static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO5_IO03;
static void __iomem *SW_PAD_GPIO5_IO03;
static void __iomem *GPIO5_DR;
static void __iomem *GPIO5_GDIR;

/* define dtsled cdev struct */
struct dtsled_dev{
    dev_t   devid;
    struct cdev cdev;
    struct class *class;
    struct device *device;
    int major;
    int minor;
    struct device_node *nd;
};
struct dtsled_dev dtsled;

static void led_switch(u8 sta)
{
    u32 val = 0;

    if(sta == LED_ON){
        val = readl(GPIO5_DR);
        val &= ~(1<<3);
        writel(val, GPIO5_DR);
    } else if (sta == LED_OFF) {
        val = readl(GPIO5_DR);
        val |= (1<<3);
        writel(val, GPIO5_DR);
    }
}

static int led_open(struct inode *inode, struct file *filp)
{
    return 0;
}

static ssize_t led_read(struct file *filp, char __user *buf,
                        size_t cnt, loff_t *offt)
{
    return 0;
}

static ssize_t led_write(struct file *filp, const char __user *buf,
                        size_t cnt, loff_t* offt)
{
    u8 databuf[1];
    u8 sta;
    int retvalue;

    retvalue = copy_from_user(databuf, buf, cnt);
    if(retvalue < 0){
        printk("kernel write data failed!\n");
        return -1;
    }
    sta = databuf[0];

    if(sta == LED_ON){
        led_switch(LED_ON);
    } else if(sta == LED_OFF){
        led_switch(LED_OFF);
    }
    return 0;            
}

static int led_release(struct inode *inode, struct file *flip)
{
    return 0;
}


static struct file_operations dtsled_fops = {
    .owner = THIS_MODULE,
    .open = led_open,
    .read = led_read,
    .write = led_write,
    .release = led_release,    
};

static int __init led_init(void)
{
    struct property *proper;
    int ret;
    const char *str;
    u32 regdata[14];
    u32 val = 0;
    
    /* 1.get device node from device tree */
    dtsled.nd = of_find_node_by_path("/tinyled");
    if(dtsled.nd == NULL) {
        printk("tinyled node can not found!\n");
        return -EINVAL;
    } else {
        printk("tinyled node has been found.\n");
    }

    /* 2.get compatible value */
    proper = of_find_property(dtsled.nd, "compatible", NULL);
    if(proper == NULL) {
        printk("get compatible failed!\n");
    } else {
        printk("compatible = %s.\n", (char *)proper->value);
    }

    /* 3.get status value */
    ret = of_property_read_string(dtsled.nd, "status", &str);
    if(ret < 0){
        printk("status read failed!\n");
    } else {
        printk("status = %s.\n", str);
    }

    /* 4.get reg val */
    ret = of_property_read_u32_array(dtsled.nd, "reg", regdata, 10);
    if(ret < 0){
        printk("get reg value failed!\n");
        return -1;
    } else {
        u8 i = 0;
        printk("reg data:\n");
        for(i = 0; i<10; i++)
            printk("%#X ",regdata[i]);
        printk("\n");
    }

    /* init led */
    /* 1.get dts gpio register remap address */
#if 0
    IMX6U_CCM_CCGR1 = ioremap(regdata[0], regdata[1]);
    SW_MUX_GPIO5_IO03 = ioremap(regdata[2], regdata[3]);
    SW_PAD_GPIO5_IO03 = ioremap(regdata[4], regdata[5]);
    GPIO5_DR = ioremap(regdata[6], regdata[7]);
    GPIO5_GDIR = ioremap(regdata[8], regdata[9]);
#else
    IMX6U_CCM_CCGR1 = of_iomap(dtsled.nd, 0);
    SW_MUX_GPIO5_IO03 = of_iomap(dtsled.nd, 1);
    SW_PAD_GPIO5_IO03 = of_iomap(dtsled.nd, 2);
    GPIO5_DR = of_iomap(dtsled.nd, 3);
    GPIO5_GDIR = of_iomap(dtsled.nd, 4);
#endif
    /* 2.enable gpio5 clock */
    val = readl(IMX6U_CCM_CCGR1);
    val &= ~(3<<30);
    val |= (3<<30);
    writel(val, IMX6U_CCM_CCGR1);

    /* 3.set gpio5_io03 mux function */
    writel(5, SW_MUX_GPIO5_IO03);
    writel(0x10b0, SW_PAD_GPIO5_IO03);

    /* 4.set gpio5_io03 as output pin */
    val = readl(GPIO5_GDIR);
    val &= ~(1<<3);
    val |= (1<<3);
    writel(val, GPIO5_GDIR);
    
    /* 5.set gpio5_io03 output1 turn off led*/
    val = readl(GPIO5_DR);
    val |= (1<<3);
    writel(val, GPIO5_DR);

    /* register chrdev */
    /* 1.alloc device id */
    if(dtsled.major){
        dtsled.devid = MKDEV(dtsled.major, 0);
        register_chrdev_region(dtsled.devid, DTSLED_CNT, DTSLED_NAME);
    } else {
        alloc_chrdev_region(&dtsled.devid, 0, DTSLED_CNT, DTSLED_NAME); /* 申请设备号 */ 
        dtsled.major = MAJOR(dtsled.devid); /* 获取分配号的主设备号 */ 
        dtsled.minor = MINOR(dtsled.devid); /* 获取分配号的次设备号 */
    }
    printk("dtsled major=%d,minor=%d\r\n",dtsled.major, dtsled.minor);

    /* 2.cdev init */
    dtsled.cdev.owner = THIS_MODULE;
    cdev_init(&dtsled.cdev, &dtsled_fops);
    /* 3.add cdev */
    cdev_add(&dtsled.cdev, dtsled.devid, DTSLED_CNT);

    /* 4.create class */
    dtsled.class = class_create(THIS_MODULE, DTSLED_NAME);
    if(IS_ERR(dtsled.class)){
        return PTR_ERR(dtsled.class);
    }

    /* 5.create device */
    dtsled.device = device_create(dtsled.class, NULL, dtsled.devid,
                                    NULL, DTSLED_NAME);
    if(IS_ERR(dtsled.device)){
        return PTR_ERR(dtsled.device);
    }

    return 0;
}

static void __exit led_exit(void)
{
    /* 取消映射 */ 
    iounmap(IMX6U_CCM_CCGR1); 
    iounmap(SW_MUX_GPIO5_IO03); 
    iounmap(SW_PAD_GPIO5_IO03); 
    iounmap(GPIO5_DR); 
    iounmap(GPIO5_GDIR);

    /* 注销字符设备驱动 */ 
    cdev_del(&dtsled.cdev);/* 删除cdev */ 
    unregister_chrdev_region(dtsled.devid, DTSLED_CNT);/*注销设备号*/ 
    
    device_destroy(dtsled.class, dtsled.devid); 
    class_destroy(dtsled.class);
}

module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Tiny");

